package com.idlecode.service;

import com.idlecode.bean.DataBaseEntity;
import com.idlecode.config.GenDataSource;
import com.idlecode.config.query.AbstractQuery;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-05-08 10:32
 **/
public class DataBaseService {


    public static List<DataBaseEntity> getDataBaseList(GenDataSource dataSource) {
        List<DataBaseEntity> dataBaseList = new ArrayList<>();
        try {
            AbstractQuery query = dataSource.getDbQuery();
            String dbNamesSql = query.dbNamesSql();
            //查询数据
            PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement(dbNamesSql);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                DataBaseEntity dataBaseEntity = new DataBaseEntity();
                dataBaseEntity.setOwner(rs.getString(query.dbName()));
                dataBaseList.add(dataBaseEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dataBaseList;
    }
}
